package com.gjt.dlmall.product.app;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gjt.dlmall.product.entity.BrandEntity;
import com.gjt.dlmall.product.service.BrandService;
import com.gjt.dlmall.product.service.CategoryService;
import com.gjt.dlmall.product.vo.BrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gjt.dlmall.product.entity.CategoryBrandEntity;
import com.gjt.dlmall.product.service.CategoryBrandService;
import com.gjt.common.utils.R;



/**
 * 分类品牌关系表
 *
 * @author gjt
 * @email g45852139@163.com
 * @date 2021-09-02 23:10:21
 */
@RestController
@RequestMapping("product/categorybrand")
public class CategoryBrandController {
    @Autowired
    private CategoryBrandService categoryBrandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    /**
     * 获取分类关联的品牌
     * @return
     */
    @GetMapping("/brands/list")
    private R relationBrandList(@RequestParam("categoryId") Integer categoryId){
       List<BrandEntity> brandEntities = categoryBrandService.getBrandByCatrgoryId(categoryId);
        List<BrandVO> collect = brandEntities.stream().map(brandEntity -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(brandEntity.getId());
            brandVO.setBrandName(brandEntity.getName());
            return brandVO;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);

    }
    /**
     * 列表
     */
    @RequestMapping("/category/list")
    //@RequiresPermissions("product:categorybrand:list")
    public R list(@RequestParam("brandId") Long brandId){
        //PageUtils page = categoryBrandService.queryPage(params);
       List<CategoryBrandEntity> data = categoryBrandService.list(new QueryWrapper<CategoryBrandEntity>().eq("brand_id",brandId));
       data.forEach(categoryBrandEntity -> {
           categoryBrandEntity.setCategoryName(categoryService.getById(categoryBrandEntity.getCategoryId()).getName());
           categoryBrandEntity.setBrandName(brandService.getById(categoryBrandEntity.getBrandId()).getName());
       });
        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrand:info")
    public R info(@PathVariable("id") Integer id){
		CategoryBrandEntity categoryBrand = categoryBrandService.getById(id);

        return R.ok().put("categoryBrand", categoryBrand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrand:save")
    public R save(@RequestBody CategoryBrandEntity categoryBrand){
		categoryBrandService.save(categoryBrand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrand:update")
    public R update(@RequestBody CategoryBrandEntity categoryBrand){
		categoryBrandService.updateById(categoryBrand);

        return R.ok();
    }

    /**
     * 删除
     *//*
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrand:delete")
    public R delete(@RequestBody Integer[] ids){
		categoryBrandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }*/
    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrand:delete")
    public R delete(@RequestBody CategoryBrandEntity categoryBrandEntity){
		categoryBrandService.remove(new QueryWrapper<CategoryBrandEntity>().eq("brand_id",categoryBrandEntity.getBrandId()).eq("category_id",categoryBrandEntity.getCategoryId()));

        return R.ok();
    }


}
